/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.engine;

import com.android1.amarena2d.commons.BlendFunction;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;

public class GL {

    static GL10 gl10;

    public static void translate(float x, float y) {
        gl10.glTranslatef(x, y, 0.0F);
    }

    public static void rotate(float d) {
        gl10.glRotatef(-d, 0.0f, 0.0f, 1.0f);
    }

    public static void scale(float scaleX, float scaleY) {
        gl10.glScalef(scaleX, scaleY, 1.0f);
    }

    public static void loadIdentity() {
        gl10.glLoadIdentity();
    }

    public static void pushMatrix() {
        gl10.glPushMatrix();
    }

    public static void popMatrix() {
        gl10.glPopMatrix();
    }

    public static void reset() {
        currentColor.set(Color.WHITE);
        texture2dEnabled = false;
        activeTexture = 0;
        cullFaceEnabled = false;
        ditheringEnabled = true;
        mulitsampleEnabled = true;
        depthTestEnabled = true;
        lightningEnabled = true;
        depthMaskEnabled = false;
        blendEnabled = false;
        blendSrc = 0;
        blendDst = 0;
    }

    public static void glViewport(int x, int y, int width, int height) {
        gl10.glViewport(x, y, width, height);
    }


    private static final Color currentColor = new Color(Color.WHITE);
    private static boolean colorInvalid;

    public static void invalidateColor() {
        colorInvalid = true;
    }

    public static void setColor(Color color) {
        if (colorInvalid || currentColor.a != color.a || currentColor.r != color.r || currentColor.g != color.g || currentColor.b != color.b) {
            currentColor.a = color.a;
            currentColor.r = color.r;
            currentColor.g = color.g;
            currentColor.b = color.b;
            gl10.glColor4f(color.r, color.g, color.b, color.a);
            colorInvalid = false;
        }
    }

    public static void clearColor(Color color) {
        gl10.glClearColor(color.r, color.g, color.b, color.a);
    }

    public static void clear() {
        gl10.glClear(GL10.GL_COLOR_BUFFER_BIT);
    }

    private static boolean texture2dEnabled = false;

    public static void enableTexture2D() {
        if (!texture2dEnabled) {
            gl10.glEnable(GL10.GL_TEXTURE_2D);
            texture2dEnabled = true;
        }
    }

    public static void disableTexture2D() {
        if (texture2dEnabled) {
            gl10.glDisable(GL10.GL_TEXTURE_2D);
            texture2dEnabled = false;
        }
    }

    private static int activeTexture = 0;

    public static void activeTexture(int i) {
        if (activeTexture != i) {
            gl10.glActiveTexture(i);
            activeTexture = i;
        }
    }

    private static boolean cullFaceEnabled = false;

    public static void enableCullFace() {
        if (!cullFaceEnabled) {
            gl10.glEnable(GL10.GL_CULL_FACE);
            cullFaceEnabled = false;
        }
    }

    public static void disableCullFace() {
        if (cullFaceEnabled) {
            gl10.glDisable(GL10.GL_CULL_FACE);
            cullFaceEnabled = true;
        }
    }

    private static boolean ditheringEnabled = true;

    public static void enableDithering() {
        if (!ditheringEnabled) {
            gl10.glEnable(GL10.GL_DITHER);
            ditheringEnabled = true;
        }
    }

    public static void disableDithering() {
        if (ditheringEnabled) {
            gl10.glDisable(GL10.GL_DITHER);
            ditheringEnabled = false;
        }
    }

    private static boolean mulitsampleEnabled = true;

    public static void enableMulitsampling() {
        if (!mulitsampleEnabled) {
            gl10.glEnable(GL10.GL_MULTISAMPLE);
            mulitsampleEnabled = true;
        }
    }

    public static void disableMulitsample() {
        if (mulitsampleEnabled) {
            gl10.glDisable(GL10.GL_MULTISAMPLE);
            mulitsampleEnabled = false;
        }
    }

    private static boolean depthTestEnabled = true;

    public static void enableDepthTest() {
        if (!depthTestEnabled) {
            gl10.glEnable(GL10.GL_DEPTH_TEST);
            depthTestEnabled = true;
        }
    }

    public static void disableDepthTest() {
        if (depthTestEnabled) {
            gl10.glDisable(GL10.GL_DEPTH_TEST);
            depthTestEnabled = false;
        }
    }

    private static boolean lightningEnabled = true;

    public static void enableLightning() {
        if (!lightningEnabled) {
            gl10.glEnable(GL10.GL_LIGHTING);
            lightningEnabled = true;
        }
    }

    public static void disableLightning() {
        if (lightningEnabled) {
            gl10.glDisable(GL10.GL_LIGHTING);
            lightningEnabled = false;
        }
    }

    private static boolean depthMaskEnabled;

    public static void enableDepthMask() {
        if (!depthMaskEnabled) {
            gl10.glDepthMask(true);
            depthMaskEnabled = true;
        }
    }

    public static void disableDepthMask() {
        if (depthMaskEnabled) {
            gl10.glDepthMask(false);
            depthMaskEnabled = false;
        }
    }

    private static int blendSrc = 0;
    private static int blendDst = 0;

    public static void blendFunc(BlendFunction blendFunction) {
        if (blendSrc != blendFunction.src || blendDst != blendFunction.dst) {
            blendSrc = blendFunction.src;
            blendDst = blendFunction.dst;
            gl10.glBlendFunc(blendFunction.src, blendFunction.dst);
        }
    }

    private static boolean blendEnabled = false;

    public static void enableBlend() {
        if (!blendEnabled) {
            gl10.glEnable(GL10.GL_BLEND);
            blendEnabled = true;
        }
    }

    public static void disableBlend() {
        if (blendEnabled) {
            gl10.glDisable(GL10.GL_BLEND);
            blendEnabled = false;
        }
    }

    public static GL10 getGl10() {
        return gl10;
    }
}
